/**
 * 表单渲染
 */
;
(function(global, factory) {
	"use strict";
	if(typeof define === "function" && define.amd) {
		define(['jquery', "jqueryui", 'bootstrapFormHelper', "util/formElementManager",
		  "flatpickr/flatpickr", "flatpickr/zh"], function(a, b, c, d, e){
			return factory(global, a, b, c, d, e);
		});
	} else {
		factory(global, $);
	}
})(typeof window !== "undefined" ? window : this, 
function(window, $, jqueryui, 
	bootstrapFormHelper, formElementManager,
	flatpickr, flatpickrZh ) {
	
	if (typeof $ === 'undefined') {
		throw new Error('requires jQuery');
	}
	
	//console.log(flatpickr);
	
	//console.log(typeof flatpickr);
	
	if(typeof flatpickr !== 'undefined' ) {
		console.log(flatpickr.l10ns);
		flatpickr.localize(flatpickr.l10ns.zh);
		flatpickr.l10ns.default.firstDayOfWeek = 1; // 星期一
		flatpickr.l10ns.default.time_24hr = true;
	}
	
	/**
	 * 控件渲染入口
	 * scopeSelector： 范围控制选择器(不能是控件本身的选择器，如果是控件本身，需要直接调用对应的控件渲染方法)
	 * forceFlag： 强制重新渲染
	 */
	let render = (scopeSelector, forceFlag) => {
		console.log("渲染页面组件");
		if(!scopeSelector) { scopeSelector = $("body"); }
		let _rerender = (_obj, flag) => {
			if(!$(_obj).data('rederedflag') || flag) {
				$(_obj).data('rederedflag', true);
				return true;
			}
			return false;
		}
		try{
			//颜色选择器
			$(".bfh-colorpicker", scopeSelector).each(function(_n, _obj){
				if(_rerender(_obj, forceFlag)) {
					render_colorPicker(_obj);
				}
			});
		}catch(e){
			console.log(e);
		}
		
		try{
			//日期选择器
			$(".form-datepicker", scopeSelector).each(function(_n, _obj){
				if(_rerender(_obj, forceFlag)) {
					render_datePicker(_obj);
				}
			});
		}catch(e){
			console.log(e);
		}
		
	}
	
	/**
	 * 根据一生成的html元素渲染页面
	 */
	let renderByHtml = function(html) {
		if(!html) {
			return ;
		}
		let ghost = this;
		$("#layout-main").html(html);
		$("#layout-main .content-column").each(function() {
			ghost.renderLayoutWrapper($(this));
		});
		$("#layout-main .draggable-element").each(function() {
			ghost.renderLayoutElement($(this));
		});
	}
	/**
	 * 表单容器
	 * @param {Object} helper
	 */
	let renderLayoutWrapper = function(helper) {
		
		if(!helper.find(".btn-wrapper").length) {
			let btnHtml = '';
			btnHtml += '<span class="btn-wrapper">';
			btnHtml += ' <span title="拖动" class="btn-drag drag"><i class="glyphicon glyphicon-move"></i></span>';
			btnHtml += ' <span title="删除" class="btn-remove remove-content-column"><i class="glyphicon glyphicon-trash"></i></span>';
			btnHtml += '</span>';
			helper.prepend(btnHtml);
		}
		
	}
	/**
	 * 表单元素
	 * @param {Object} helper
	 */
	let renderLayoutElement = function(helper) {
		if(!helper.find(".btn-wrapper").length) {
			let btnHtml = '';
			btnHtml += '<span class="btn-wrapper">';
			btnHtml += ' <span title="删除" class="btn-remove remove-draggable-element"><i class="glyphicon glyphicon-trash"></i></span>';
			btnHtml += '</span>';
			helper.prepend(btnHtml);
		}
		let componentName = helper.attr('data-conp-name');
		if(componentName) {
			let tmpInstance = helper.data("componentInstance");
			if(typeof tmpInstance !== 'object') {
				// 从页面已存在的html生成对象实例
				let domId = helper.children("[id]").first().attr("id");
				tmpInstance = new window[componentName](domId);
				helper.data("componentInstance", tmpInstance);
			}
			let propertyId = helper.attr("data-property-id");
			if(propertyId) {
				let isViewPage = (pageKind ?? "") === "view";
				let property = formElementManager.getModelProperty(propertyId);
				tmpInstance.propertyId = propertyId;//setter
				tmpInstance.property = property;//setter
				tmpInstance.forceReadonly(isViewPage || property.readonly);
			}
			// 拖拽后隐藏左侧列表中的字段（删除后要重新显示出来）
			tmpInstance.shown(true);
			return tmpInstance;
		}
		
	}
	//颜色选择器
	let render_colorPicker = function(dom) {
		let jdom = $(dom);
		if(!jdom.data("p202110181431")) {
			jdom.bfhcolorpicker();
		}
    	jdom.data("p202110181431", true);
    	return jdom;
	}
	
	/**
	 * 日期选择器
	 */
	let render_datePicker = function(dom) {
		let jdom = $(dom);
		try{
			
			if(!jdom.hasClass("form-datepicker")) {
				jdom = $(dom).find(".form-datepicker");
			}
			
			if(typeof flatpickr === 'undefined') {
				console.log("flatpickr is undefined");
				return jdom;
			}
			
			let val = jdom.val();
			
			if(!val) {
				let tmp_default = jdom.attr("data-defaultDate");
				if(tmp_default == "now") {
					val = new Date();
				}
			}
			
			let dateType = jdom.attr("data-dateType") ?? 'dt';
			let options = {
				"dateFormat": jdom.attr("data-dateFormat") ?? 'Y-m-d H:i',
				//"defaultDate": "today",
				"allowInput": true
			}
			if(val) {
				options.defaultDate = val;
			}
			//是否启用时间选择器
			options.enableTime = (options.dateFormat.indexOf('H') != -1);
			
			//是否启用日期选择器
			//options.noCalendar = (options.dateFormat.indexOf('Y') == -1);
			options.noCalendar = dateType == 't';
			
			//是否启用秒
			options.enableSeconds = options.enableTime && (options.dateFormat.indexOf('S') != -1);
			
			//console.log(options);
			let flatpickrIns = jdom.data("flatpickrIns");
			if(flatpickrIns) {
				flatpickrIns.destroy();
			}
			//if(!flatpickrIns) {
				//flatpickrIns = jdom.flatpickr(options);
				flatpickrIns = flatpickr(jdom, options);
				jdom.data("flatpickrIns", flatpickrIns);
			//} else {
				/*for(let k1 in options ) {
					let k = k1 + "";
					console.log(k);
					console.log(options[k]);
					flatpickrIns.set(k, options[k]);
				}*/
			//}
		}catch(e){
			console.log(e);
		}
    	return jdom;
	}
	
	
	//下拉选
	
	
	
	
	let formRender = {
		"render": render,
		"renderByHtml": renderByHtml,
		"renderLayoutWrapper": renderLayoutWrapper,
		"renderLayoutElement": renderLayoutElement
	};
	
	formRender.renderColorPicker = render_colorPicker;
	formRender.render_datePicker = render_datePicker;
	
	return formRender;
});